package dk.hewison.client.album;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import dk.hewison.client.Log;
import dk.hewison.client.domain.Directory;
import dk.hewison.client.panels.CompositeView;
import dk.hewison.client.panels.PhotoListHolder;

import java.util.Iterator;
import java.util.List;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-07 20:27:07 +0000 (Sat, 07 Feb 2009) $:
 * @version $Revision: 362 $:
 */
public class AlbumSheet extends CompositeView<List<Directory>> implements LoadListener {

    ScrollPanel scrollPanel;
    List<Directory> directories;
    HorizontalPanel panel;
    //private NEffectPanel effectPanel;

    int imageCount = 0;

    int loadedCount = 0;

    long start;
    private int defaultHeight;

    public AlbumSheet() {
    }

    public List<Directory> getModel() {
        return directories;
    }

    public void initView() {
        /* Fade fadeIn = new Fade();
        fadeIn.getProperties().setStartOpacity(0);
        fadeIn.getProperties().setEndOpacity(100);*/
        //effectPanel = new NEffectPanel();
        //effectPanel.addEffect(fadeIn);
        panel = new HorizontalPanel();

        //panel.setWidth("100%");

        scrollPanel = new ScrollPanel(panel);
        scrollPanel.setStyleName("albumsheet");
        initWidget(scrollPanel);
        //effectPanel.add(panel);
    }

    public void setModel(List<Directory> directories) {

        setVisible(directories != null && !directories.isEmpty());
        resize();

        start = System.currentTimeMillis();
        this.directories = directories;

        redraw();

        Iterator<Widget> iterator = panel.iterator();
        while (iterator.hasNext()) {
            PhotoListHolder photoListHolder = (PhotoListHolder) iterator.next();
            photoListHolder.loadThumbnails();
        }


    }


    private void redraw() {
        panel.clear();
        imageCount = directories.size();
        for (Directory directory : directories) {
            panel.add(createPhotoListHolder(directory));
        }
    }

    private Widget createPhotoListHolder(Directory directory) {
        return new PhotoListHolder(directory, this);
    }

    public void fadeIn() {
        //effectPanel.playEffects();
    }

    /**
     * Fired when a widget encounters an error while loading.
     *
     * @param sender the widget sending the event.
     */
    public void onError(Widget sender) {

    }

    /**
     * Fired when a widget finishes loading.
     *
     * @param sender the widget sending the event.
     */
    public void onLoad(Widget sender) {
        loadedCount++;
        if (loadedCount == imageCount) {
            long took = System.currentTimeMillis() - start;
            Log.debug("Took " + took + " ms to load " + imageCount + " user.");
            //Window.alert("Took " + took + " ms to load " + imageCount + " user.");
        }
    }

    public Widget getMyWidget() {
        return panel;
    }

    public void resize() {
        int width = Window.getClientWidth();
        defaultHeight = 180;
        //todo if the horizontal scrollbar is visible we need to add 10px to the height

        if (!isVisible()) {
            defaultHeight = 0;
        }
        scrollPanel.setPixelSize(width, defaultHeight);
    }
}